package top.zywork.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.zywork.annotation.SysLog;
import top.zywork.common.BeanUtils;
import top.zywork.common.BindingResultUtils;
import top.zywork.common.StringUtils;
import top.zywork.dto.PagerDTO;
import top.zywork.dto.SearchHistoryDTO;
import top.zywork.query.SearchHistoryQuery;
import top.zywork.security.SecurityUtils;
import top.zywork.service.SearchHistoryService;
import top.zywork.vo.PagerVO;
import top.zywork.vo.ResponseStatusVO;
import top.zywork.vo.SearchHistoryVO;

import java.util.List;

/**
 * SearchHistoryController控制器类<br/>
 *
 * 创建于2019-10-18<br/>
 *
 * @author http://zywork.top 王振宇
 * @version 1.0
 */
@RestController
@RequestMapping("/search-history")
@Slf4j
public class SearchHistoryController extends BaseController {

    private SearchHistoryService searchHistoryService;

    @PostMapping("admin/save")
    @SysLog(description = "后台保存SearchHistoryVO")
    public ResponseStatusVO save(@RequestBody @Validated SearchHistoryVO searchHistoryVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        searchHistoryService.save(BeanUtils.copy(searchHistoryVO, SearchHistoryDTO.class));
        return ResponseStatusVO.ok("添加成功", null);
    }

    @PostMapping("admin/batch-save")
    @SysLog(description = "后台批量保存SearchHistoryVO")
    public ResponseStatusVO saveBatch(@RequestBody @Validated List<SearchHistoryVO> searchHistoryVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        searchHistoryService.saveBatch(BeanUtils.copyListObj(searchHistoryVOList, SearchHistoryDTO.class));
        return ResponseStatusVO.ok("批量添加成功", null);
    }

    @GetMapping("admin/remove/{id}")
    @SysLog(description = "后台删除SearchHistoryVO")
    public ResponseStatusVO removeById(@PathVariable("id") Long id) {
        searchHistoryService.removeById(id);
        return ResponseStatusVO.ok("删除成功", null);
    }

    @PostMapping("admin/batch-remove")
    @SysLog(description = "后台批量删除SearchHistoryVO")
    public ResponseStatusVO removeByIds(@RequestBody String[] ids) {
        searchHistoryService.removeByIds(StringUtils.strArrayToLongArray(ids));
        return ResponseStatusVO.ok("批量删除成功", null);
    }

    @PostMapping("admin/update")
    @SysLog(description = "后台更新SearchHistoryVO")
    public ResponseStatusVO update(@RequestBody @Validated SearchHistoryVO searchHistoryVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        int updateRows = searchHistoryService.update(BeanUtils.copy(searchHistoryVO, SearchHistoryDTO.class));
        if (updateRows == 1) {
            return ResponseStatusVO.ok("更新成功", null);
        } else {
            return ResponseStatusVO.dataError("数据版本号有问题，在此更新前数据已被更新", null);
        }
    }

    @PostMapping("admin/batch-update")
    @SysLog(description = "后台批量更新SearchHistoryVO")
    public ResponseStatusVO updateBatch(@RequestBody @Validated List<SearchHistoryVO> searchHistoryVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        searchHistoryService.updateBatch(BeanUtils.copyListObj(searchHistoryVOList, SearchHistoryDTO.class));
        return ResponseStatusVO.ok("批量更新成功", null);
    }

    @PostMapping("admin/active")
    @SysLog(description = "后台激活或冻结SearchHistoryVO")
    public ResponseStatusVO active(@RequestBody SearchHistoryVO searchHistoryVO) {
        searchHistoryService.update(BeanUtils.copy(searchHistoryVO, SearchHistoryDTO.class));
        return ResponseStatusVO.ok("激活或冻结成功", null);
    }

    @PostMapping("admin/batch-active")
    @SysLog(description = "后台批量激活或冻结SearchHistoryVO")
    public ResponseStatusVO activeBatch(@RequestBody @Validated List<SearchHistoryVO> searchHistoryVOList) {
        searchHistoryService.updateBatch(BeanUtils.copyListObj(searchHistoryVOList, SearchHistoryDTO.class));
        return ResponseStatusVO.ok("批量激活或冻结成功", null);
    }

    @GetMapping("admin/one/{id}")
    public ResponseStatusVO getById(@PathVariable("id") Long id) {
        SearchHistoryVO searchHistoryVO = new SearchHistoryVO();
        Object obj = searchHistoryService.getById(id);
        if (obj != null) {
            searchHistoryVO = BeanUtils.copy(obj, SearchHistoryVO.class);
        }
        return ResponseStatusVO.ok("查询成功", searchHistoryVO);
    }

    @GetMapping("admin/all")
    public ResponseStatusVO listAll() {
        PagerDTO pagerDTO = searchHistoryService.listAll();
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), SearchHistoryVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/all-cond")
    public ResponseStatusVO listAllByCondition(@RequestBody SearchHistoryQuery searchHistoryQuery) {
        PagerDTO pagerDTO = searchHistoryService.listAllByCondition(searchHistoryQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), SearchHistoryVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/pager-cond")
    public ResponseStatusVO listPageByCondition(@RequestBody SearchHistoryQuery searchHistoryQuery) {
        PagerDTO pagerDTO = searchHistoryService.listPageByCondition(searchHistoryQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), SearchHistoryVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    /**
     * 用户保存搜索历史
     * @param searchHistoryVO
     * @param bindingResult
     * @return
     */
    @PostMapping("user/save")
    @SysLog(description = "用户保存搜索历史")
    public ResponseStatusVO userSave(@RequestBody @Validated SearchHistoryVO searchHistoryVO, BindingResult bindingResult) {
        searchHistoryVO.setUserId(SecurityUtils.getJwtUser().getUserId());
        return save(searchHistoryVO, bindingResult);
    }

    /**
     * 用户查询搜索历史
     * @param searchHistoryQuery
     * @return
     */
    @PostMapping("user/pager-cond")
    public ResponseStatusVO userListPageByCondition(@RequestBody SearchHistoryQuery searchHistoryQuery) {
        searchHistoryQuery.setUserId(SecurityUtils.getJwtUser().getUserId());
        return listPageByCondition(searchHistoryQuery);
    }

    /**
     * 用户查询自己的指定数量的历史关键词
     * @param total
     * @return
     */
    @GetMapping("user/his-keys/{total}")
    public ResponseStatusVO userListHisKeywordsByCount(@PathVariable("total") int total) {
        List<String> hisKeywords = searchHistoryService.listUserHisKeywordsByCount(SecurityUtils.getJwtUser().getUserId(), total);
        return ResponseStatusVO.ok("查询成功", hisKeywords);
    }

    /**
     * 用户查询指定数量的热门搜索关键词
     * @param total
     * @return
     */
    @GetMapping("user/hot-keys/{total}")
    public ResponseStatusVO userListHotKeywordsByCount(@PathVariable("total") int total) {
        List<String> hotKeywords = searchHistoryService.listHotKeywordsByCount(total);
        return ResponseStatusVO.ok("查询成功", hotKeywords);
    }

    /**
     * 用户删除自己的所有搜索历史
     * @return
     */
    @GetMapping("user/remove-all")
    @SysLog(description = "用户删除所有搜索历史")
    public ResponseStatusVO userRemoveAllByUserId() {
        searchHistoryService.removeAllByUserId(SecurityUtils.getJwtUser().getUserId());
        return ResponseStatusVO.ok("删除成功", null);
    }

    /**
     * 用户删除自己的指定关键词的搜索历史
     * @return
     */
    @GetMapping("user/remove-by-keyword/{keyword}")
    @SysLog(description = "用户删除指定搜索历史")
    public ResponseStatusVO userRemoveByKeyword(@PathVariable("keyword") String keyword) {
        searchHistoryService.removeByKeyword(SecurityUtils.getJwtUser().getUserId(), keyword);
        return ResponseStatusVO.ok("删除成功", null);
    }

    @Autowired
    public void setSearchHistoryService(SearchHistoryService searchHistoryService) {
        this.searchHistoryService = searchHistoryService;
    }
}
